package com.htdc.web.controller;


import com.htdc.web.bean.Agreement;
import com.htdc.web.bean.Bill;
import com.htdc.web.bean.Company;
import com.htdc.web.bean.ProjectTb;
import com.htdc.web.service.AgreementService;
import com.htdc.web.service.BillService;
import com.htdc.web.service.ProjectService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private AgreementService agreementService;


    @RequestMapping("/payhome")//主框架
    private String paymentHome(HttpServletRequest request){
        System.err.println("正在浏览支付网页");
        String url=request.getRequestURI();
        System.err.println(url);
        List<Company> companies =projectService.getAllCompanies();
        Map<Company,Map<ProjectTb,List<Agreement>>> CPAMap=new HashMap<>();
        for (Company company : companies) {
            List<ProjectTb> projects = projectService.getProjectByCid((int)company.getCid());
            Map<ProjectTb,List<Agreement>> PAMap = new HashMap<>();
            for (ProjectTb project : projects) {
                List<Agreement>agreementList= agreementService.showAllAgreementByPid((int) project.getPid());
                System.err.println("打印项目ID"+project.getPid());
                for (Agreement agreement:agreementList){
                    System.err.println(agreement);
                }
                PAMap.put(project,agreementList);
            }
            CPAMap.put(company,PAMap);
        }
        request.setAttribute("CPAMap",CPAMap);
        return "agreement/billIndex";
    }

    @RequestMapping("/unpaid")//查看未支付账单
    private  String showUnpaidBill(HttpServletRequest request,HttpSession session,@Param("pid")Integer pid){
        List<Agreement> agreements=agreementService.showAllAgreementByPid(pid);
        List<Bill> unpaidBills=new ArrayList<>();
        for (Agreement agreement : agreements) {
            unpaidBills.addAll(billService.showUnpaidBillsByAid(agreement.getAid()));
            System.out.println("打印pid下所有账单1："+unpaidBills);
        }
        session.setAttribute("unpaidBills",unpaidBills);
        session.setAttribute("pid",pid);
        return "agreement/unpaidBill";
    }


    @RequestMapping("/paid")//查看历史已付费账单
    private String showAllPaidBills(HttpServletRequest request, HttpSession session, @Param("pid")Integer pid){
        List<Agreement> agreements=agreementService.showAllAgreementByPid(pid);
        List<Bill> paidBills=new ArrayList<>();
        for (Agreement agreement : agreements) {
            paidBills.addAll(billService.showAllPaidByAid(agreement.getAid()));
            System.out.println("打印pid下所有账单2："+paidBills);
        }
        request.setAttribute("paidBills",paidBills);
        session.setAttribute("pid",pid);
       return "agreement/paidBill";
    }



    @RequestMapping("/fda")//支付账单
    private String payment(){//通过传账户号的集合修改账单
        //@Param("bids")List<String> bids
        //通过时间戳获取当前时间取得支付时间
        Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        List<String> b=new ArrayList<>();
        b.add("1720180325Mon Jan 01 00:00:00 CST 2018");
        billService.payment(b,sqlDate);
        return "agreement/unpaidBill";
    }
}
